package homework;

import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//随机生成50个男生姓名(年龄 20-25)，50个女生姓名(18-22) 格式:
//金胤霆-男-23
//邓希勤-女-18
//龙思安-女-22
//汪兆毅-男-25
//赵希勤-男-22
//沈伯特-女-22

public class Test10 {
	public static void main(String[] args) throws Exception {
		String fnameStr="https://www.jimingbao.com/90981.html";
		String bnameStr="http://www.fwsir.com/ziliao/html/ziliao_20230101131842_2181723.html";
		String gnameStr="http://www.fwsir.com/ziliao/html/ziliao_20231115100157_3428778.html";
		
		String webCrawlerXing = webCrawler(fnameStr,0);
		String webCrawlerGirl = webCrawler(gnameStr,1);
		String webCrawlerBoy = webCrawler(bnameStr,1);

		ArrayList<String> fnameData = getData(webCrawlerXing,"(.、){2}");
		ArrayList<String> gnameData = getData(webCrawlerGirl,"(..、){2}..");
		ArrayList<String> bnameData = getData(webCrawlerBoy,"(..，){4}..");

		ArrayList<String> fnameList=new ArrayList();
		getName(fnameData, fnameList, 0);
		ArrayList<String> bnameList=new ArrayList();
		getName(bnameData, bnameList, 1);
		ArrayList<String> gnameList=new ArrayList();
		getName(gnameData, gnameList, 0);
		
		ArrayList<String> boyName = rondomName(fnameList,bnameList,1);
		System.out.println(boyName);
		ArrayList<String> girlName = rondomName(fnameList,gnameList,0);
		System.out.println(girlName);
		
		FileWriter fw=new FileWriter("D:\\FileTest\\name.txt");
		for(String ls:boyName){
			fw.write(ls);
			fw.write("\n");
		}
		for(String ls:girlName){
			fw.write(ls);
			fw.write("\n");
		}
		fw.close();
	}

	private static ArrayList<String> rondomName(ArrayList<String> List1,
			ArrayList<String> List2, int sex) {
		// TODO Auto-generated method stub
		HashSet<String> set=new HashSet<>();
		Random r = new Random();
		int age = 0;
		String rdSex = null;
		while(true){
			if(set.size()==50){
				break;
			}
			if(sex == 1) {
				age = r.nextInt(6)+20;
				rdSex = "男";
			}else if(sex == 0) {
				age = r.nextInt(5)+18;
				rdSex = "女";
			}
			Collections.shuffle(List2);
			Collections.shuffle(List1);
			set.add(List1.get(0)+List2.get(0)+"-"+rdSex+"-"+age+"-"+"1");
		}
		ArrayList<String> list=new ArrayList<>(set);
		return list;
	}

	private static ArrayList<String> getData(String str, String regex) {
		// TODO Auto-generated method stub
		ArrayList<String> list=new ArrayList<>();
		Pattern pattern=Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		while(matcher.find()){
			String group = matcher.group();
			list.add(group);
		}
		return list;
	}

	private static String webCrawler(String net, int idx) throws Exception {
		// TODO Auto-generated method stub
		
		StringBuilder sb = new StringBuilder();
		URL url = new URL(net);
		URLConnection conn = url.openConnection();
		InputStreamReader isr = null;
		if(idx==1){
			isr = new InputStreamReader(conn.getInputStream(),"gbk");
		}
		else {
			isr = new InputStreamReader(conn.getInputStream(),"utf-8");
		}
		int ch;
		while((ch=isr.read())!=-1){
			sb.append((char)ch);
		}
		return sb.toString();
	}
	
	private static void getName(ArrayList<String> list1,ArrayList<String> list2, int idx) {
		for (String ls : list1) {
			String[] name;
			if(idx==1){
				name = ls.split("，");
			}
			else{
				name = ls.split("、");
			}
			for(int i=0;i<name.length;i++){
				list2.add(name[i]);
			}
		}
	}
	
}
